package com.servlet.music;

import com.dao.BannerService;
import com.dao.impl.BannerServiceImpl;
import com.entity.Banner;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.servlet.teacher.BaseServlet;
import com.util.JsonUtil;
import com.util.ProxyUtil;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/BannerServlet")
public class BannerServlet extends BaseServlet {


	/**
	 * 查询一个轮播图
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		String id = req.getParameter("id");

		BannerService bannerService = (BannerService)ProxyUtil.getDaoProxy(new BannerServiceImpl());

		Banner banner = null;

		try {
			banner = bannerService.findOne(Integer.parseInt(id));
			if(banner == null) {
				throw new RuntimeException("不存在");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(banner);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 更新某个轮播图
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		BannerService bannerService = (BannerService)ProxyUtil.getDaoProxy(new BannerServiceImpl());

		try {
			Banner banner = new Banner();
			BeanUtils.populate(banner, req.getParameterMap());

			int update = bannerService.update(banner);
			if(update == 0) {
				throw new RuntimeException("更新失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 插入一个轮播图
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		BannerService bannerService = (BannerService)ProxyUtil.getDaoProxy(new BannerServiceImpl());
		Banner banner = new Banner();

		try {
			BeanUtils.populate(banner, req.getParameterMap());
			int insert = bannerService.insert(banner);
			if(insert == 0) {
				throw new RuntimeException("插入失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 *  查询所有未删除的轮播图
	 *
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void queryData(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		BannerService bannerService = (BannerService)ProxyUtil.getDaoProxy(new BannerServiceImpl());
		List<Banner> list = null;
		try {

			list =  bannerService.queryList();

		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		jsonUtil.setData(list);
		out.print(gson.toJson(jsonUtil));
	}

	/**
	 * 删除某一个轮播图
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		GsonBuilder gsonBuilder = new GsonBuilder();
		Gson gson = gsonBuilder.create();
		PrintWriter out = resp.getWriter();
		JsonUtil jsonUtil = new JsonUtil();

		BannerService bannerService = (BannerService)ProxyUtil.getDaoProxy(new BannerServiceImpl());
		String id = req.getParameter("id");
		try {
			if(id != null && id.length() > 0) {
				bannerService.deleteById(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
			jsonUtil.setCode(201);
			jsonUtil.setMsg(e.getMessage());
			out.print(gson.toJson(jsonUtil));
			return;
		}

		jsonUtil.setCode(200);
		out.print(gson.toJson(jsonUtil));
	}

}
